package com.lyw.springCloud.controller;

import com.lyw.springCloud.entities.Payment;
import com.lyw.springCloud.result.CommonResult;
import com.lyw.springCloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Auther liyawei
 * @Date 2022/4/17 11:46
 */

@Slf4j
@RestController
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    // 为了测试负载均衡使用的
    @Value("${server.port}")
    private String serverPort;

    /**
     *  这里需要注意的是因为是post请求前端发送的默认是json格式的对象，所以后端需要使用 @RequestBody 注解接收对象
     * @param payment
     * @return
     */
    @PostMapping("/payment/create")
    public CommonResult<Object> create(@RequestBody Payment payment) {

        int save = paymentService.create(payment);

        log.info(">>>>>>>>>>>>>>>>>>> 插入了一条语句 ：{}", payment);

        if (save > 0) {
            return new CommonResult<>(200, "插入成功, serverPort - " + serverPort, payment);
        } else {
            return new CommonResult<>(444, "插入失败", null);
        }
    }


    @GetMapping("/payment/get/{id}")
    public CommonResult<Object> findPaymentById(@PathVariable("id") Long id) {

        Payment payment = paymentService.getById(id);

        log.info(">>>>>>>>>>>>>>>>>>> 查询了一条语句 ：{}", payment);
        if (payment != null) {
            return new CommonResult<>(200, "查询成功 serverPort - " + serverPort, payment);
        } else {
            return new CommonResult<>(444, "查询失败， id + " + id, null);
        }
    }

    @GetMapping("/payment/lb")
    public String lb() {
        return serverPort;
    }


    @GetMapping("/payment/lib")
    public String lb1(@RequestParam("uname") String uname) {
        return serverPort;
    }
}
